--[[
    -debugger：             先 F5 运行插件，然后运行魔兽，输入指令 
    -add name v：           给当前选择的单位添加属性 
    -set name v：           给当前选择的单位设置属性 
    -get name v：           获取当前选择的单位属性 
    -weak：                 扣除目标当前 95% 最大生命值
    -kill：                 杀死当前选择的单位
    -show：                 开启/关闭选择单位显示基本属性 
    -removebuff name：      移除当前选择的单位的 buff
    -deldebuffs：           清除当前选择的单位所有负面效果
    -delbuffs：             清除当前选择的单位所有正面效果
    -addbuff name：         为当前选择的单位添加 buff
    -showbuff：             显示当前选择的单位的所有 buff
    -addskill name：        给当前选择的单位添加技能
    -removeskill name：     给当前选择的单位移除技能 
    -upskill name：         给当前选择的单位升级技能
    -open：                 点亮全图
    -close：                关闭全图 
    -gold v：               增加金币
    -lumber v：             增加木材
    -coin v：               增加复活币
    -wtf：                  开启/关闭无冷却无魔耗模式
    -refresh：              刷新技能冷却时间
    -cunit name：           为选择的玩家创建一个单位，如不指定 name 则创建当前选择的单位
    -ai：                   开启/关闭自动施法
    -citem name：           在当前选择的单位脚下创建指定物品
--]]
local japi = require'jass.japi'

local callback = {

    ['addskill'] = function (list, player)
        local name = list[2]
        local unit = player:get_select_unit()
        if not unit then
            return
        end
        local skill = unit:find_skill(name, '英雄')
        if skill and tonumber(list[3]) then 
            skill:set_level(tonumber(list[3]))
        else 
            unit:add_skill(name, '英雄', nil, {level = tonumber(list[3])})
        end 
        
    end,
    ['cs'] = function (list, player)
        local value = list[2]
        local unit = player:get_select_unit()
        if not unit then
            return
        end
        unit:notify('英雄-测试属性',unit, tonumber(value))
    end,
    ['jk'] = function (list, player)
        player:notify('玩家-显示商店',player,'闹鬼金矿')
    end,
    ['add'] = function (list, player)
        local name, value = list[2], list[3]
        local unit = player:get_select_unit()
        if not unit then
            return
        end
        unit:add(name, tonumber(value))
    end,
    ['set'] = function (list, player)
        local name, value = list[2], list[3]
        local unit = player:get_select_unit()
        if not unit then
            return
        end
        unit:set(name, value)
    end,
    ['get'] = function (list, player)
        local name = list[2]
        local unit = player:get_select_unit()
        if not unit then
            return
        end
        player:send_msg(('%s:%s'):format(name, unit:get(name)))
    end,
    ['wd'] = function(_, player)
        local hero = player.hero
        if hero then
            hero:event '单位-受到伤害前'(function(_,_,_,data)
                data:div(100)
            end)
            hero:event '单位-造成伤害前'(function(_,_,_,data)
                data:on_kill()
            end)
        end
    end,

    ['lv'] = function (list, player)
        local num = tonumber(list[2])
        local unit = player:get_select_unit()

        unit:set_level(num)
    end,
    ['exp'] = function (list, player)
        local num = tonumber(list[2])
        local unit = player:get_select_unit()

        unit:add_exp(num)
    end,
    
    ['kill'] = function (_, player)        
        local unit = player:get_select_unit()
        if not unit then
            return
        end
        unit:kill(unit)
    end,


    ['padd'] = function (list, player)
        local key = list[2]
        if not key then
            return
        end
        
        local number = list[3]
        number = tonumber(number)
        number = number or 1

        player:add(key, number)
    end,
    
    ['gold'] = function (list, player)
        player:add('金币', tonumber(list[2]) or 100000)
    end,
    ['lumber'] = function (list, player)
        player:add('木材', tonumber(list[2]) or 100000)
    end,
    ['xd'] = function (list, player)
        print(tonumber(list[2]) or 100000)
        player:add('杀敌', tonumber(list[2]) or 100000)
    end,
    ['cunit'] = function (list, player)
        local unit = player:get_select_unit()
        if not unit then
            return
        end

        local name = list[2]
        if not name then
            unit:create_unit(unit:get_name())
        else
            unit:create_unit(name)
        end
    end,


    ['item'] = function (list, player)
        local unit = player:get_select_unit()
        if not unit then
            return
        end

        local point = unit:get_point()
        local item = ac.item.create(list[2], point,{owner_player = player})
        local num = tonumber(list[3])
        if num then 
            item:set_count(num)
        end 
        item:give(unit)
    end,


    

    ['mouse'] = function (list, player)
        local text = class.text:builder 
        {
            x = 0,
            y = 0,
            w = 100,
            h = 32,
            text = '',
            align = 'center',
            font_size = 10,
        }

        ac.loop(33, function (t)
            if text._id == nil then 
                t:remove()
                return 
            end 

            local x, y = japi.GetMouseX(), japi.GetMouseY()
            local pos_x, pos_y = ac.hardware:get_mouse_pos()
            text:set_position(pos_x, pos_y - 36)
            local str = string.format("%.0f, %.0f\n%0.f, %.0f", x, y, pos_x, pos_y)
            text:set_text(str)
        end)
    end,
    

    ['test_start'] = function(list,player)
        ac.game:notify('测试功能开启')
    end,

}

local allow_player = {
    ['WorldEdit'] = true,
}

local mall_data = {
    --['WorldEdit'] = {
    --    ['地图等级'] = 100,
    --},
}
ac.wait(100, function ()
    ac.each_player(function (player)
        print(player:get_name())
        if allow_player[player:get_name()] then 
            require 'test.喊话工具'
        end 
    end)
end)

ac.test_trigger = ac.game:event '玩家-聊天' (function(trg, player, str)
    local name = player:get_name()
    if ALLOW_CHEAT or allow_player[name] then
        player.cheat = true
    end
    if not ac.cheat and not player.cheat then
        return
    end
    if str:sub(1, 1) ~= '-' then
        return
    end
    str = str:sub(2, str:len())
    local list = ac.split(str,' ')
    print(table.unpack(list))

    if callback[list[1]] then
        callback[list[1]](list, player)
    end
end)

local old_has_item = ac.player.__index.has_item
function ac.player.__index:has_item(key)
    -- return true
    local name = self:get_name()

    if not mall_data[name] then
        return old_has_item(self,key)
    end
    
    if not mall_data[name][key] then
        return old_has_item(self,key)
    end

    return mall_data[name][key] == 1 
end

local old_get_level = ac.player.__index.get_level
function ac.player.__index:get_level()
    local name = self:get_name()

    if not mall_data[name] then
        return old_get_level(self)
    end
    
    if not mall_data[name]['地图等级'] then
        return old_get_level(self)
    end

	return mall_data[name]['地图等级']
end
